package com.ganqiang1983.wol4j;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.ganqiang1983.wol4j.command.Startup;
import com.ganqiang1983.wol4j.util.Constants;
import com.ganqiang1983.wol4j.util.Host;

/**
 * UI启动类 <br>
 * 十分简陋的UI，只是做了简单的验证而已 <br>
 * 主要是为了方便使用 <br>
 */
public class UI extends JFrame implements ActionListener
{

  private static final long serialVersionUID = 1L;
  private int width, height;
  private JLabel macLabel, broadcastLabel, portLabel;
  private JTextField macField,broadcastField,portField;
  private JButton okButton, exitButton;

  public UI()
  {
    width = 430;
    height = 160;
    setSize(width, height);
    Toolkit tk = this.getToolkit();
    int scrWidth = tk.getScreenSize().width;
    int scrHeight = tk.getScreenSize().height;
    setLocation((scrWidth - width) / 2, (scrHeight - height) / 2);
    setResizable(false);
    initComponents();
  }

  private void initComponents()
  {
    Container container = this.getContentPane();
    container.setLayout(new GridLayout(5, 5));

    macLabel = new JLabel("目标主机MAC地址(必填)");
    broadcastLabel = new JLabel("局域网广播地址/广域网IP地址");
    portLabel = new JLabel("端口号");
    macField = new JTextField(17);
    broadcastField = new JTextField(15);
    portField = new JTextField(5);
    okButton = new JButton("确定");
    okButton.addActionListener(this);
    exitButton = new JButton("退出");
    exitButton.addActionListener(this);
    container.add(new JLabel("WOL4J"));
    container.add(new JLabel(""));
    container.add(macLabel);
    container.add(macField);
    container.add(broadcastLabel);
    container.add(broadcastField);
    container.add(portLabel);
    container.add(portField);
    container.add(okButton);
    container.add(exitButton);
  }

  public void actionPerformed(ActionEvent event)
  {
    if (event.getSource() == okButton) {
      
      String mac = macField.getText();
      String broadcast = broadcastField.getText();
      String portStr = portField.getText();
      
      if (mac == null || "".equals(mac.trim())) {
        showMsg(0,"目标主机MAC地址不能为空！");
        return;
      }
      boolean flag = Constants.validMAC(mac);
      if (!flag) {
    	showMsg(0,"请输入正确的MAC地址！");
        return;  
      }
      
      int port = 0;
      if (portStr != null && !"".equals(portStr.trim())) {
        try{
          port = Integer.parseInt(portStr);
        }catch(Exception e){
          showMsg(0,"端口号必须为正整数!");
          return;
        }
      }
      
      boolean status = load(mac,broadcast,port);  
      if(status){
        showMsg(1,"操作成功!");
      }else{
        showMsg(0,"操作失败!");
      }
    }
    if (event.getSource() == exitButton) {
      dispose();
      System.exit(0);
    }
  }
  
  private void showMsg(int status,String msg){
    JOptionPane.showMessageDialog(this, msg,
        0==status?"错误":"提示", 0==status?JOptionPane.ERROR_MESSAGE:JOptionPane.INFORMATION_MESSAGE);
  }
  
  
  private boolean load(String mac,String broadcast,int port){  
    Host host = new Host();
    host.setMac(mac);
    host.setIp(broadcast);
    host.setPort(port);
    return Startup.sendWakeUpMessage(host);
  }
 
  public static void main(String[] args)
  {
    UI frame = new UI();
    frame.setVisible(true);
  }


}
